﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using D3DSS.DataContract;
using D3DSS.Enums;
using D3DSS.FormUI.Properties;
using D3DSS.Http;
using D3DSS.DataContract.QueryStatisticsOption;

namespace D3DSS.FormUI
{
    public partial class frmQueryStatistics : Form
    {
        public frmQueryStatistics()
        {
            InitializeComponent();
        }

        private void frmQueryStatistics_Load(object sender, EventArgs e)
        {
            cmbGameServer.SelectedIndex = Settings.Default.LastStatisticsOrderGameServer;
            cmbAct.SelectedIndex = Settings.Default.LastStatisticsOrderAct;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            var request = new QueryStatisticsRequest();
            request.UserID = Program.Token.UserID;
            request.Act = (ActType)(cmbAct.SelectedIndex + 1);
            request.GameServer = (GameServerType)(cmbGameServer.SelectedIndex + 1);

            var response = HttpRequest.Post<Response<QueryStatisticsResponse>>(Settings.Default.QueryStatisticsUrl, request, Program.Token);

            if (response.Item1.IsSuccess)
            {
                lblQueryTimeValue.Text = response.Item1.Data.QueryTime.ToString() + " 毫秒";

                dgvPublicDefaultMF.DataSource = InitMFDataGridView(response.Item1.Data.PublicDefaultStatisticsResult.MFStatisticsList, false);
                dgvPublicDefaultRoomIP.DataSource = InitRoomDataGridView(response.Item1.Data.PublicDefaultStatisticsResult.RoomStatisticsList, false);
                dgvPublicDefaultTimeLine.DataSource = InitTimeDataGridView(response.Item1.Data.PublicDefaultStatisticsResult.TimeStatisticsList, false);

                dgvPublicExtendMF.DataSource = InitMFDataGridView(response.Item1.Data.PublicExtendStatisticsResult.MFStatisticsList, true);
                dgvPublicExtendRoomIP.DataSource = InitRoomDataGridView(response.Item1.Data.PublicExtendStatisticsResult.RoomStatisticsList, true);
                dgvPublicExtendTimeLine.DataSource = InitTimeDataGridView(response.Item1.Data.PublicExtendStatisticsResult.TimeStatisticsList, true);

                dgvPrivateDefaultMF.DataSource = InitMFDataGridView(response.Item1.Data.PrivateDefaultStatisticsResult.MFStatisticsList, false);
                dgvPrivateDefaultRoomIP.DataSource = InitRoomDataGridView(response.Item1.Data.PrivateDefaultStatisticsResult.RoomStatisticsList, false);
                dgvPrivateDefaultTimeLine.DataSource = InitTimeDataGridView(response.Item1.Data.PrivateDefaultStatisticsResult.TimeStatisticsList, false);

                dgvPrivateExtendMF.DataSource = InitMFDataGridView(response.Item1.Data.PrivateExtendStatisticsResult.MFStatisticsList, true);
                dgvPrivateExtendRoomIP.DataSource = InitRoomDataGridView(response.Item1.Data.PrivateExtendStatisticsResult.RoomStatisticsList, true);
                dgvPrivateExtendTimeLine.DataSource = InitTimeDataGridView(response.Item1.Data.PrivateExtendStatisticsResult.TimeStatisticsList, true);
            }
            else
            {
                MessageBox.Show(response.Item1.MessageDictionary.Single(m => m.MessageCode == response.Item1.Code).MessageContent);
            }
        }

        private DataTable InitTimeDataGridView(List<TimeStatistics> timeList, bool isExtend)
        {
            DataTable datatable = new DataTable();

            datatable.Columns.Add(new DataColumn("时间段", typeof(string)));

            if (!isExtend)
            {
                datatable.Columns.Add(new DataColumn("61稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("62稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("63稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有样本数量", typeof(long)));

                timeList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["时间段"] = m.StartHour.ToString() + ":00 - " + m.EndHour.ToString() + ":00";
                    dataRow["61稀有掉率%"] = (double)((int)(m.Level61RaraDropRate * 10000)) / 100;
                    dataRow["62稀有掉率%"] = (double)((int)(m.Level62RaraDropRate * 10000)) / 100;
                    dataRow["63稀有掉率%"] = (double)((int)(m.Level63RaraDropRate * 10000)) / 100;
                    dataRow["稀有样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }
            else
            {
                datatable.Columns.Add(new DataColumn("普通掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("魔法掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("传奇掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("样本数量", typeof(long)));

                timeList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["时间段"] = m.StartHour.ToString() + ":00 - " + m.EndHour.ToString() + ":00";
                    dataRow["普通掉率%"] = (double)((int)(m.WhiteDropRate * 10000)) / 100;
                    dataRow["魔法掉率%"] = (double)((int)(m.MagicDropRate * 10000)) / 100;
                    dataRow["稀有掉率%"] = (double)((int)(m.RaraDropRate * 10000)) / 100;
                    dataRow["传奇掉率%"] = (double)((int)(m.LegendDropRate * 10000)) / 100;
                    dataRow["样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }

            return datatable;
        }

        private DataTable InitRoomDataGridView(List<RoomStatistics> roomList, bool isExtend)
        {
            DataTable datatable = new DataTable();

            datatable.Columns.Add(new DataColumn("房间IP", typeof(string)));

            if (!isExtend)
            {
                datatable.Columns.Add(new DataColumn("61稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("62稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("63稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有样本数量", typeof(long)));

                roomList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["房间IP"] = m.RoomParentIP.ToString() + "." + m.RoomIP.ToString();
                    dataRow["61稀有掉率%"] = (double)((int)(m.Level61RaraDropRate * 10000)) / 100;
                    dataRow["62稀有掉率%"] = (double)((int)(m.Level62RaraDropRate * 10000)) / 100;
                    dataRow["63稀有掉率%"] = (double)((int)(m.Level63RaraDropRate * 10000)) / 100;
                    dataRow["稀有样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }
            else
            {
                datatable.Columns.Add(new DataColumn("普通掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("魔法掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("传奇掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("样本数量", typeof(long)));

                roomList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["房间IP"] = m.RoomParentIP.ToString() + "." + m.RoomIP.ToString();
                    dataRow["普通掉率%"] = (double)((int)(m.WhiteDropRate * 10000)) / 100;
                    dataRow["魔法掉率%"] = (double)((int)(m.MagicDropRate * 10000)) / 100;
                    dataRow["稀有掉率%"] = (double)((int)(m.RaraDropRate * 10000)) / 100;
                    dataRow["传奇掉率%"] = (double)((int)(m.LegendDropRate * 10000)) / 100;
                    dataRow["样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }

            return datatable;
        }

        private DataTable InitMFDataGridView(List<MFStatistics> mfList, bool isExtend)
        {
            DataTable datatable = new DataTable();

            datatable.Columns.Add(new DataColumn("MF区间", typeof(string)));

            if (!isExtend)
            {
                datatable.Columns.Add(new DataColumn("61稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("62稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("63稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有样本数量", typeof(long)));

                mfList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["MF区间"] = m.MinMF.ToString() + " - " + m.MaxMF.ToString();
                    dataRow["61稀有掉率%"] = (double)((int)(m.Level61RaraDropRate * 10000)) / 100;
                    dataRow["62稀有掉率%"] = (double)((int)(m.Level62RaraDropRate * 10000)) / 100;
                    dataRow["63稀有掉率%"] = (double)((int)(m.Level63RaraDropRate * 10000)) / 100;
                    dataRow["稀有样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }
            else
            {
                datatable.Columns.Add(new DataColumn("普通掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("魔法掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("稀有掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("传奇掉率%", typeof(double)));
                datatable.Columns.Add(new DataColumn("样本数量", typeof(long)));

                mfList.ForEach(m =>
                {
                    var dataRow = datatable.NewRow();
                    dataRow["MF区间"] = m.MinMF.ToString() + " - " + m.MaxMF.ToString();
                    dataRow["普通掉率%"] = (double)((int)(m.WhiteDropRate * 10000)) / 100;
                    dataRow["魔法掉率%"] = (double)((int)(m.MagicDropRate * 10000)) / 100;
                    dataRow["稀有掉率%"] = (double)((int)(m.RaraDropRate * 10000)) / 100;
                    dataRow["传奇掉率%"] = (double)((int)(m.LegendDropRate * 10000)) / 100;
                    dataRow["样本数量"] = m.Sample;
                    datatable.Rows.Add(dataRow);
                });
            }

            return datatable;
        }
    }
}
